<?php
/**
 * created by zzy
 * date: 2017/11/27 10:46
 */

namespace App\Http\Admin\Appset\Controllers;
use App\Eloquent\Ygt\Logistics;
use Framework\BaseClass\Http\Admin\Controller;
use App\Eloquent\Ygt\Appindex;

class IndexController extends Controller
{
    public function index()
    {
    }
    public function lists()
    {
//        $admin_user         = request()->user('admin')->toArray();
//        $company_id         = $admin_user['company_id'];
        if (request()->isMethod('post')) {

            $where          = [];
            $fields         = 'id,pid,type,title,img_url,img_url_active,is_sale,is_show_help,help_href';
            $lists          = Appindex::getList( $where,$fields,'','',['id','asc'] );
            foreach ( $lists as $key=>$val ){
                $http_img_url   = $http_img_url_active = '';
                if( $val['img_url'] ){
                    $http_img_url                   = asset($val['img_url']);
                }
                if( $val['img_url_active'] ){
                    $http_img_url_active            = asset($val['img_url_active']);
                }
                $lists[$key]['http_img_url']             = $http_img_url;
                $lists[$key]['http_img_url_active']     = $http_img_url_active;
            }
            $data_json      = $lists->toJson();
            return $data_json;
        } else {
            return $this->view('lists', compact('company_json'));
        }
    }

    public function edit()
    {
        $adminUser          = request()->user('admin')->toArray();
        $companyId          = $adminUser['company_id'];

        $params             = $this->requestJson();
        $id                 = isset($params['id']) ? intval($params['id']) : 0;
        $pid                = isset($params['pid']) ? intval($params['pid']) : 0;
        $type               = isset($params['type']) ? intval($params['type']) : 0;
        $title              = isset($params['title']) ? $params['title'] : '';
        $imgUrl             = isset($params['img_url']) ? $params['img_url'] : '';
        $imgUrlActive       = isset($params['img_url_active']) ? $params['img_url_active'] : '';
        $isSale             = isset($params['is_sale']) ? intval($params['is_sale']) : 0;
        $help_href       = isset($params['help_href']) ? $params['help_href'] : '';
        $is_show_help             = isset($params['is_show_help']) ? intval($params['is_show_help']) : 0;
        if( $type==0 )
        {
            return $this->ajaxFail( 'type值必须' );
        }
        if( $title=='' )
        {
            return $this->ajaxFail( '标题必须' );
        }
        if( $imgUrl=='' )
        {
            return $this->ajaxFail( '图标必须' );
        }
        $tableData          = [
            'pid'=>$pid,
            'type'=>$type,
            'title'=>$title,
            'img_url'=>$imgUrl,
            'img_url_active'=>$imgUrlActive,
            'is_sale'=>$isSale,
            'help_href'=>$help_href,
            'is_show_help'=>$is_show_help
        ];
        if($id>0){
            $where              = ['id'=>$id];
            $result             = Appindex::updateOneData($where,$tableData);
        } else {
            $result             = Appindex::insertOneData($tableData);
        }
        if( $result ){
            return $this->ajaxSuccess( '成功' );
        } else {
            return $this->ajaxFail( '失败' );
        }
    }
    public function delete()
    {
    }
}